package cn.bjit.manager.impl;

import cn.bjit.manager.ShortLinkManager;
import cn.bjit.mapper.ShortLinkMapper;
import cn.bjit.model.ShortLinkDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class ShortLinkManagerImpl implements ShortLinkManager {

    @Resource
    private ShortLinkMapper shortLinkMapper;

    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {

        return shortLinkMapper.insert(shortLinkDO);
    }

    @Override
    public ShortLinkDO findByShortLinCode(String shortLinkCode) {

        ShortLinkDO shortLinkDO = shortLinkMapper.selectOne(
                new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode).eq("del",0));
        return shortLinkDO;
    }

//    public int del(String shortLinkCode, Long accountNo) {
//        ShortLinkDO shortLinkDO = new ShortLinkDO();
//        shortLinkDO.setDel(1);
//        int rows = shortLinkMapper.update(shortLinkDO,
//                new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode).eq("account_no", accountNo));
//        return rows;
//    }

    @Override
    public int del(ShortLinkDO shortLinkDO) {


        int rows = shortLinkMapper.update(null,
                new UpdateWrapper<ShortLinkDO>()
                        .eq("code", shortLinkDO.getCode())
                        .eq("account_no", shortLinkDO.getAccountNo())
                        .set("del",1));
        return rows;
    }

    @Override
    public int update(ShortLinkDO shortLinkDO) {

        int rows = shortLinkMapper.update(null, new UpdateWrapper<ShortLinkDO>()
                .eq("code", shortLinkDO.getCode())
                .eq("del", 0)
                .eq("account_no",shortLinkDO.getAccountNo())

                .set("title", shortLinkDO.getTitle())
                .set("domain", shortLinkDO.getDomain()));


        return rows;
    }

}